# coding=utf-8
__author__ = 'st316'
'''
You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A (say ai, ai+1 ... aj) is called a "repeated sequence" if it appears more than once in A (there exists some positive k that ai+k = ai, ai+k+1 = ai+1, ... aj+k = aj) and its appearances are not intersected (i + k > j).

Can you find the longest repeated sequence in A?

输入
Line 1: n (1 <= n <= 300), the length of A.
Line 2: the sequence, a1 a2 ... an (0 <= ai <= 100).

输出
The length of the longest repeated sequence.

样例输入
5
2 3 2 3 2
样例输出
2
'''


class Solution:
    # @param sequence, a list of numbers
    # @return longestLength
    def longestLength(self, a):
        s = []
        n = len(a)
        ret = 0
        s.append(a[0])
        for i in xrange(1, n):
            s.append(s[i - 1] + a[i])
        left = 0
        right = n - 1
        while left <= right:
            mid = (left + right) / 2
            if self.check(mid, a, s):
                ret = mid
                left = mid + 1
            else:
                right = mid - 1
        return ret

    def check(self, mid, a, s):
        n = len(a)
        k = 0
        for i in xrange(0, n):
            for j in xrange(i + mid, n):
                if n - j - 1 < mid:
                    break
                tmp1 = s[i + mid - 1] - (s[i - 1] if i != 0 else 0)
                tmp2 = s[j + mid - 1] - (s[j - 1] if j != 0 else 0)
                if tmp1 != tmp2:
                    continue
                flag = True
                for k in xrange(mid):
                    if a[i + k] != a[j + k]:
                        flag = False
                        break
                if flag:
                    return True
        return False


if __name__ == '__main__':
    s = Solution()
    A = [2, 3, 2, 3, 2]
    print s.longestLength(A)